<?php

declare(strict_types=1);

namespace app\shop\controller\admin\order;

use think\facade\Db;
use think\Request;
use sheep\controller\Backend;
use sheep\exception\SheepException;
use app\shop\model\order\Invoice as OrderInvoiceModel;
use app\shop\model\order\Order as OrderModel;

class Invoice extends Backend
{

    public function initialize()
    {
        $this->model = new OrderInvoiceModel;
    }

    /**
     * 发票列表
     *
     * @return \think\Response
     */
    public function list()
    {
        $invoices = $this->model->sheepFilter()->with(['user', 'order', 'order_items'])
            ->paginate(request()->param('list_rows', 10))->each(function ($invoice) {
                $invoice->order_status = $invoice->order_status;
                $invoice->order_status_text = $invoice->order_status_text;
                $invoice->order_fee = $invoice->order_fee;
            });

        return success('获取成功', $invoices);
    }



    public function confirm(Request $request, $id) 
    {
        $params = $request->param();
        $invoice = $this->model->waiting()->where('id', $id)->findOrFail();

        $invoice->download_urls = $params['download_urls'] ?? null;
        $invoice->invoice_amount = $params['invoice_amount'];
        $invoice->status = 'finish';
        $invoice->finish_time = time();
        $invoice->save();

        return success('开具成功', $invoice);
    }
}
